﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Text.RegularExpressions;

namespace Cms.Web.CmsSystem.SysModules.PopWin
{
    public partial class TreeGrid : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string treestr = "<div region=\"west\" split=\"true\" style=\"width: 300px;\"><table id=\"treegrid\" fit=true></table></div>";
                string treestrs = "<div region=\"center\" split=\"true\" style=\"width: 300px;\"><table id=\"treegrid\" fit=true></table></div>";
                string datastr = "<div region=\"center\" style=\"padding: 5px; background: #eee; z-index: 2;\"><table id=\"gridlist\" fit=true></table></div>";
                if (DialogConfig != null)
                {
                    switch (DialogConfig.Type)
                    {
                        case 1:
                            LitDataGrid.Text = datastr + BuilderList();
                            break;
                        case 2:
                            LitTreeGrid.Text = treestrs + BuilderTree();
                            break;
                        case 12:
                            LitTreeGrid.Text = treestr + BuilderTree();
                            LitDataGrid.Text = datastr + BuilderList();// +"<script type='text/javascript'>$(function(){$(body).layout();});</script>";
                            break;
                    }
                    Page.Title = DialogConfig.Title;//设置标题
                }
            }
        }

        public Model.Sys_Popwin DialogConfig
        {
            get
            {
                if (ViewState["dialogconfig"] == null)
                {
                    string sid = EntLib.Request.GetString("id");
                    Model.Sys_Popwin model = new Model.Sys_Popwin();
                    BLL.BaseDao<Model.Sys_Popwin, string> blldialog = new BLL.BaseDao<Model.Sys_Popwin, string>();
                    if (!String.IsNullOrEmpty(sid))
                    {
                        model = blldialog.GetById(sid, false);
                    }
                    ViewState["dialogconfig"] = model;
                }
                return (Model.Sys_Popwin)ViewState["dialogconfig"];
            }
        }
        public string BuilderTree()
        {
            if (DialogConfig == null)
                return "";
            string exparam = "";
            if (DialogConfig.Treesql.IndexOf("[@") > -1)
            {
                exparam = GetParamBySql(DialogConfig.Treesql);
            }

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script type='text/javascript' >");
            sb.AppendLine("$(function(){");
            //  sb.AppendLine("using('treegrid',function(){");
            sb.AppendLine("$('#treegrid').treegrid({");
            sb.AppendFormat("   width: '100%',\r\n");
            sb.AppendFormat("   url: '/Tools/Json.ashx?param=dialog&type=tree&id={0}{1}',", DialogConfig.Id, exparam);
            sb.AppendFormat("   idField: '{0}',\r\n", DialogConfig.Treekeyfield);
            sb.AppendFormat("   treeField:'{0}',", DialogConfig.TreeField);
            if (DialogConfig.Type == 2)
            {
                if (DialogConfig.Muilt)
                {
                    sb.Append("   singleSelect:false,");
                    sb.Append("   checkbox:true,");
                }
            }
            else
            {
                sb.Append("   singleSelect:true,");
            }
            sb.AppendFormat("   rownumbers: false,");
            sb.AppendFormat("   remoteSort: false,");
            sb.AppendFormat("   columns: [[");
            if (DialogConfig.Type == 2 && DialogConfig.Muilt)
            {
                sb.Append("{");
                sb.AppendFormat("		 field: 'ck',checkbox:true");
                sb.Append("},");
            }
            Iesi.Collections.Generic.ISet<Model.Sys_Popwindetail> listM = DialogConfig.Detail;//(new BLL.NPlant.ColConfig()).GetModelList(String.Format("tablesid='{0}-tree' order by [ord] asc", DialogConfig.code));
            sb.Append(BuilderColumn(listM, false));
            sb.AppendFormat("	]],");
            sb.Append("   onClickRow: function(rowData) {");
            sb.AppendFormat("selectdata(rowData,'{0}');    ", DialogConfig.TreeAssignfield);
            sb.Append("   },onLoadSuccess:function(row, data){treeloadsuccess(row, data);}");
            sb.Append("});");
            sb.AppendLine("});");
            sb.AppendLine("</script>");
            return sb.ToString();
        }
        public string GetParamBySql(string sql)
        {
            StringBuilder sb = new StringBuilder();
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"\[[^\]]*\]");
            MatchCollection mc = reg.Matches(sql);
            foreach (Match m in mc)
            {
                if (m.Value.IndexOf("@") > -1)
                {
                    string p = m.Value.Replace("[@", "").Replace("]", "");
                    sb.AppendFormat("&{0}={1}", p, Request.Params[p]);
                }
            }
            return sb.ToString();
        }
        public string BuilderColumn(Iesi.Collections.Generic.ISet<Model.Sys_Popwindetail > listM, bool sort)
        {
            StringBuilder sb = new StringBuilder();
            foreach (Model.Sys_Popwindetail cc in listM)
            {

                sb.Append("{");
                sb.AppendFormat("		 field: '{0}', title: '{1}', width: {2},sortable:{4} ,hidden:{3}", cc.Fieldname, cc.Title, String.IsNullOrEmpty(cc.Length)?"50":cc.Length, (!cc.Visibled).ToString().ToLower(), sort ? "true" : "false");
                sb.Append("},");
            }
            return sb.ToString().TrimEnd(',');
        }
        public string BuilderList()
        {
            if (DialogConfig == null)
                return "";
            StringBuilder sb = new StringBuilder();
            sb.Append("<script type='text/javascript' >");
            sb.AppendLine("try{");
            sb.Append("$(function(){");
            //   sb.AppendLine("using('datagrid',function(){");
            sb.Append("$('#gridlist').datagrid({");
            sb.AppendFormat("   width: '100%',");
            //bool firstgetdata = false;
            //5 是部门,用户列表
            /*if (DialogConfig.sid == 5 || DialogConfig.firstgetdata)
            {
                firstgetdata = true;
            }
            if (DialogConfig.ListType == 1)
                firstgetdata = false;*/
            string exparam = "";
            if (DialogConfig.Listsql.IndexOf("[@") > -1)
            {
                exparam = GetParamBySql(DialogConfig.Listsql);
            }

            sb.AppendFormat("   url: '/Tools/Json.ashx?param=dialog&type=list&id={0}{1}',", DialogConfig.Id, exparam);
            sb.AppendFormat("   idField: '{0}',", DialogConfig.Listkeyfield );
            sb.AppendFormat("   singleSelect:{0},", (!DialogConfig.Muilt).ToString().ToLower());
            sb.AppendFormat("   rownumbers: false,");
            sb.AppendFormat("   remoteSort: false,");
            sb.AppendFormat("   checkbox:{0},", DialogConfig.Muilt.ToString().ToLower());
            sb.AppendFormat("   columns: [[");
            if (DialogConfig.Muilt)
            {
                sb.Append("{");
                sb.AppendFormat("		 field: 'ck',checkbox:true ");
                sb.Append("},");
            }

            Iesi.Collections.Generic.ISet<Model.Sys_Popwindetail> listM = DialogConfig.Detail;// (new BLL.NPlant.ColConfig()).GetModelList(String.Format("tablesid='{0}-list' order by [ord] asc", DialogConfig.code));
            sb.Append(BuilderColumn(listM, true));
            sb.AppendFormat("	]],");
            sb.Append("   onDblClickRow: function(rowData) {");
            sb.AppendFormat("      ");
            sb.Append("   }");
            sb.Append("});");
            sb.AppendLine("});");
            sb.AppendLine("}catch(e){}");
            sb.AppendLine("</script>");
            
            return sb.ToString();
        }
    }
}
